using System;
using FlatKit;
using UnityEngine;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class TreadmillController : MonoBehaviour
{
	public bool IsPlayer = true;

	private float multiplier;

	public GameObject Visuals;

	public GameObject[] extraVisuals;

	public Item TreadmillItem;

	public UvScrollerMat scroll;

	public UvScrollerMat[] extraScroll;

	[SerializeField]
	private bool right;

	private void OnTriggerStay(Collider other)
	{
		if (other.CompareTag("Player") && other.attachedRigidbody != null)
		{
			other.attachedRigidbody.AddForce((!right) ? Vector3.right : (Vector3.left * multiplier));
		}
	}

	private void Start()
	{
		base.gameObject.SetActive(Preloader.ShopConfig.UpgradeShop);
		if (Preloader.ShopConfig.UpgradeShop)
		{
			GameManager instance = SingletonMB<GameManager>.Instance;
			instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(GameStarted));
			GameObject[] array = extraVisuals;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].SetActive(value: false);
			}
			for (int j = 0; j < InventoryManager.Instance.GetInventoryCount(TreadmillItem); j++)
			{
				if (j < extraVisuals.Length)
				{
					if ((bool)extraVisuals[j])
					{
						extraVisuals[j].SetActive(value: true);
					}
					if ((bool)extraScroll[j])
					{
						extraScroll[j].speedX = Mathf.Lerp(0.5f, 1f, (float)InventoryManager.Instance.GetInventoryCount(TreadmillItem) / 20f) * -2f;
					}
				}
			}
		}
		if (SingletonMB<GameManager>.Instance.IsDuelMode)
		{
			if (IsPlayer)
			{
				DuelModeManager instance2 = SingletonMB<DuelModeManager>.Instance;
				instance2.OnUpgradedPlayer = (Action)Delegate.Combine(instance2.OnUpgradedPlayer, new Action(OnItemPurchase));
			}
			else
			{
				DuelModeManager instance3 = SingletonMB<DuelModeManager>.Instance;
				instance3.OnUpgradedOpponent = (Action)Delegate.Combine(instance3.OnUpgradedOpponent, new Action(OnItemPurchase));
			}
		}
		InventoryManager instance4 = InventoryManager.Instance;
		instance4.OnItemPurchased = (Action)Delegate.Combine(instance4.OnItemPurchased, new Action(OnItemPurchase));
	}

	private void OnItemPurchase()
	{
		GameStarted();
	}

	private void OnDestroy()
	{
		InventoryManager instance = InventoryManager.Instance;
		instance.OnItemPurchased = (Action)Delegate.Remove(instance.OnItemPurchased, new Action(OnItemPurchase));
	}

	private void GameStarted()
	{
		if (SingletonMB<GameManager>.Instance.IsDuelMode)
		{
			if (IsPlayer)
			{
				multiplier = (float)SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Treadmill * 0.1f;
				Visuals.SetActive(SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Treadmill > 0);
				scroll.speedX = Mathf.Lerp(0.5f, 6f, (float)SingletonMB<DuelModeManager>.Instance.PlayerUpgrades.Treadmill / 20f) * -1f;
			}
			else
			{
				multiplier = (float)(-SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Treadmill) * 0.1f;
				Visuals.SetActive(SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Treadmill > 0);
				scroll.speedX = Mathf.Lerp(0.5f, 6f, (float)SingletonMB<DuelModeManager>.Instance.OpponentUpgrades.Treadmill / 20f) * -1f;
			}
		}
		else
		{
			if (!Preloader.ShopConfig.UpgradeShop)
			{
				return;
			}
			multiplier = (float)InventoryManager.Instance.GetInventoryCount(TreadmillItem) * 0.02f;
			GameObject[] array = extraVisuals;
			for (int i = 0; i < array.Length; i++)
			{
				array[i].SetActive(value: false);
			}
			for (int j = 0; j < InventoryManager.Instance.GetInventoryCount(TreadmillItem); j++)
			{
				Debug.Log("INDEX " + j);
				if (extraVisuals.Length > j)
				{
					if ((bool)extraVisuals[j])
					{
						extraVisuals[j].SetActive(value: true);
					}
					if ((bool)extraScroll[j])
					{
						extraScroll[j].speedX = Mathf.Lerp(0.5f, 1f, (float)InventoryManager.Instance.GetInventoryCount(TreadmillItem) / 20f) * -2f;
					}
				}
			}
		}
	}

	public void DrillBoosterModeStart()
	{
		int num = InventoryManager.Instance.GetInventoryCount(TreadmillItem);
		if (InventoryManager.Instance.GetInventoryCount(TreadmillItem) < 8)
		{
			num = 8;
		}
		multiplier = (float)num * 0.1f;
		Visuals.SetActive(num > 0);
		scroll.speedX = Mathf.Lerp(0.5f, 6f, (float)num / 20f) * -1f;
	}

	public void DrillBoosterModeFinish()
	{
		GameStarted();
	}
}
